﻿namespace EyeOpen.Security.Service.Test
{
    using System;
    using EyeOpen.Security.Server.BootStrapper;
    using EyeOpen.Security.Test.Helper;
    using EyeOpen.Test.Patch;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using SharpTestsEx;

    [TestClass]
    public class SecurityServiceTest : TestBase
    {
        [TestInitialize]
        public void Initialize()
        {
            new SecurityDatabaseFactory().CreateDatabase();
        }

        [TestMethod]
        [Owner("Matteo Migliore")]
        [Description("Integration.Security.Service")]
        [ExpectedException(typeof(ArgumentException))]
        public void VerifyCredentialWithInvalidEmailLengthExpectedException()
        {
            using (var securityService = new SecurityServiceFactory().CreateServiceContext())
            {
                securityService.IsValidCredential(UserKnownValues.ShortEmail, UserKnownValues.StrongPassword);
            }
        }

        [TestMethod]
        [Owner("Matteo Migliore")]
        [Description("Integration.Security.Service")]
        public void CreateUserAndCredentialCheckTryToAuthenticateExpectValidCredentialsAccepted()
        {
            using (var securityService = new SecurityServiceFactory().CreateServiceContext())
            {
                securityService.SaveUser(Guid.NewGuid(), UserKnownValues.BillEmail, UserKnownValues.StrongPassword);
            }

            using (var securityService = new SecurityServiceFactory().CreateServiceContext())
            {
                securityService
                    .IsValidCredential(UserKnownValues.BillEmail, UserKnownValues.StrongPassword)
                    .Should("Inserted User and Credential, login failed.")
                    .Be
                    .True();
            }
        }

        [TestMethod]
        [Owner("Matteo Migliore")]
        [Description("Integration.Security.Service")]
        public void TryWrongCredentialsExpectNotValidCredentials()
        {
            using (var securityService = new SecurityServiceFactory().CreateServiceContext())
            {
                securityService
                    .IsValidCredential(UserKnownValues.BillEmail, UserKnownValues.WrongPassword)
                    .Should("Inserted wrong password, login successful.")
                    .Be
                    .False();
            }
        }
    }
}